﻿using System;
using System.Linq;
using LeagueSharp;
using LeagueSharp.Common;
using SharpDX;
using Color = System.Drawing.Color;

namespace UltimateCarry
{
	class Lulu : Champion
	{
		public Spell Q;
		public Spell QPix;
		public Spell W;
		public Spell E;
		public Spell R;

        public Lulu()
        {
			LoadMenu();
			LoadSpells();

			Drawing.OnDraw += Drawing_OnDraw;
			Game.OnUpdate += Game_OnGameUpdate;
			Game.OnSendPacket += Game_OnGameSendPacket;
			Interrupter.OnPossibleToInterrupt += Interrupter_OnPosibleToInterrupt;
			PluginLoaded();
		}

		private void LoadMenu()
		{
			Program.Menu.AddSubMenu(new Menu("团队作战", "TeamFight"));
			Program.Menu.SubMenu("TeamFight").AddItem(new MenuItem("useQ_TeamFight", "使用 Q").SetValue(true));
			Program.Menu.SubMenu("TeamFight").AddItem(new MenuItem("useW_TeamFight_assist", "使用 W 协助").SetValue(true));
			Program.Menu.SubMenu("TeamFight").AddItem(new MenuItem("useE_TeamFight_Aggro", "使用 E 优先仇恨").SetValue(false));
			Program.Menu.Item("useE_TeamFight_Aggro").ValueChanged += SwitchAggro;
			Program.Menu.SubMenu("TeamFight").AddItem(new MenuItem("useE_TeamFight_Passiv", "使用 E 优先被动").SetValue(true));
			Program.Menu.Item("useE_TeamFight_Passiv").ValueChanged += SwitchPassiv;
			Program.Menu.SubMenu("TeamFight").AddItem(new MenuItem("useR_TeamFight", "使用智能 R").SetValue(true));

			Program.Menu.AddSubMenu(new Menu("骚扰", "Harass"));
			Program.Menu.SubMenu("Harass").AddItem(new MenuItem("useQ_Harass", "使用 Q").SetValue(true));
			Program.Menu.SubMenu("Harass").AddItem(new MenuItem("useE_Harass", "使用 E").SetValue(true));
			Program.Menu.SubMenu("Harass").AddItem(new MenuItem("useE_Harass_pix", "使用 E 再 Q").SetValue(true));
			AddManaManager("Harass", 40);

			Program.Menu.AddSubMenu(new Menu("清兵", "LaneClear"));
			Program.Menu.SubMenu("LaneClear").AddItem(new MenuItem("useQ_LaneClear", "使用 Q").SetValue(true));
			AddManaManager("LaneClear", 0);

			Program.Menu.AddSubMenu(new Menu("支持模式", "Passive"));
			Program.Menu.SubMenu("Passive").AddItem(new MenuItem("useW_Interupt", "W 打断技能").SetValue(false));

			Program.Menu.AddSubMenu(new Menu("支持模式", "SupportMode"));
			Program.Menu.SubMenu("SupportMode").AddItem(new MenuItem("hitMinions", "击中小兵").SetValue(false));

			Program.Menu.AddSubMenu(new Menu("绘制", "Drawing"));
			Program.Menu.SubMenu("Drawing").AddItem(new MenuItem("Draw_Disabled", "禁用全部").SetValue(false));
			Program.Menu.SubMenu("Drawing").AddItem(new MenuItem("Draw_Q", "绘制 Q").SetValue(true));
			Program.Menu.SubMenu("Drawing").AddItem(new MenuItem("Draw_W", "绘制 W").SetValue(true));
			Program.Menu.SubMenu("Drawing").AddItem(new MenuItem("Draw_E", "绘制 E").SetValue(true));
			Program.Menu.SubMenu("Drawing").AddItem(new MenuItem("Draw_R", "绘制 R").SetValue(true));
			Program.Menu.SubMenu("Drawing").AddItem(new MenuItem("Draw_pix", "绘制 Pix").SetValue(true));
			
		}

		private void SwitchAggro(object sender, OnValueChangeEventArgs e)
		{
			if (e.GetNewValue<bool>())
				Program.Menu.Item("useE_TeamFight_Passiv").SetValue(false);
		}

		private void SwitchPassiv(object sender, OnValueChangeEventArgs e)
		{
			if(e.GetNewValue<bool>())
				Program.Menu.Item("useE_TeamFight_Aggro").SetValue(false);
		}

		private void LoadSpells()
		{
			Q = new Spell(SpellSlot.Q, 945);
			Q.SetSkillshot(0.25f,50,1450,false,SkillshotType.SkillshotLine);

			QPix = new Spell(SpellSlot.Q, 945);
			QPix.SetSkillshot(0.25f, 50, 1450, false, SkillshotType.SkillshotLine,PixPosition(),PixPosition());
			
			W = new Spell(SpellSlot.W, 650);

			E = new Spell(SpellSlot.E, 650);

			R = new Spell(SpellSlot.R, 900);
		
		}

		private static Vector3 PixPosition()
		{
			foreach (var pix in ObjectManager.Get<Obj_AI_Minion>().Where(pix => pix.Name == "RobotBuddy" && pix.IsAlly))
				return pix.Position;
			return ObjectManager.Player.Position;
		}

		private void Interrupter_OnPosibleToInterrupt(Obj_AI_Base unit, InterruptableSpell spell)
		{
			if(!Program.Menu.Item("useW_Interupt").GetValue<bool>())
				return;
			if(ObjectManager.Player.Distance(unit) < W.Range && W.IsReady() && unit.IsEnemy )
				W.Cast(unit, Packets());
		}

		private void Game_OnGameUpdate(EventArgs args)
		{
			switch(Program.Orbwalker.ActiveMode)
			{
				case Orbwalking.OrbwalkingMode.Combo:
					if (Program.Menu.Item("useQ_TeamFight").GetValue<bool>())
					{
                        Cast_BasicLineSkillshot_Enemy(Q, TargetSelector.DamageType.Magical);
                        Cast_BasicLineSkillshot_Enemy(QPix, PixPosition(), TargetSelector.DamageType.Magical);
					}
					if (Program.Menu.Item("useW_TeamFight_assist").GetValue<bool>())
						Cast_Speedboost_onFriend(W);
					if (Program.Menu.Item("useE_TeamFight_Aggro").GetValue<bool>() ||
					    Program.Menu.Item("useE_TeamFight_Passiv").GetValue<bool>())
						Cast_E();
					if (Program.Menu.Item("useR_TeamFight").GetValue<bool>())
						Cast_R();
					break;
				case Orbwalking.OrbwalkingMode.Mixed:
					if(Program.Menu.Item("useQ_Harass").GetValue<bool>())
					{
                        Cast_BasicLineSkillshot_Enemy(Q, TargetSelector.DamageType.Magical);
                        Cast_BasicLineSkillshot_Enemy(QPix, PixPosition(), TargetSelector.DamageType.Magical);	
					}
					if(Program.Menu.Item("useE_Harass").GetValue<bool>())
                        Cast_onEnemy(E, TargetSelector.DamageType.Magical);
					if(Program.Menu.Item("useE_Harass_pix").GetValue<bool>() && EnoughManaFor(SpellSlot.Q,SpellSlot.E) && Q.IsReady())
                        Cast_onMinion_nearEnemy(E, Q.Range, TargetSelector.DamageType.Magical);
					break;
				case Orbwalking.OrbwalkingMode.LaneClear:
					if(Program.Menu.Item("useQ_LaneClear").GetValue<bool>())
						Cast_BasicLineSkillshot_AOE_Farm(Q);

					break;
			}
		}

		private void Cast_R()
		{
			if(!R.IsReady())
				return;
            foreach (var friend in from friend in Program.Helper.OwnTeam.Where(hero => hero.Distance(ObjectManager.Player) <= R.Range)
                                   let enemyCount = Program.Helper.EnemyTeam.Count(hero => hero.Distance(friend) <= 600)
                                   let enemyCountnear = Program.Helper.EnemyTeam.Count(hero => hero.Distance(friend) <= 200)
								  let frinedhealthprecent = friend.Health / friend.MaxHealth * 100
								  where frinedhealthprecent <= 1 || 
								  (frinedhealthprecent <= 20 && enemyCount >= 1) ||
								  (frinedhealthprecent <= 25 && enemyCount >= 2) ||
								  (frinedhealthprecent <= 30 && enemyCountnear >= 2)
								  select friend)
			{
				R.CastOnUnit(friend, Packets());
				return;
			}
		}

		private void Cast_E()
		{
			var healatpercent = Program.Menu.Item("useE_TeamFight_Passiv").GetValue<bool>() ? 70 : 35;
			var attack = Program.Menu.Item("useE_TeamFight_Aggro").GetValue<bool>();
			Cast_Shield_onFriend(E,healatpercent);
			if (attack)
                Cast_onEnemy(E, TargetSelector.DamageType.Magical);
		}

		private void Drawing_OnDraw(EventArgs args)
		{

			if(Program.Menu.Item("Draw_Disabled").GetValue<bool>())
				return;

			if(Program.Menu.Item("Draw_pix").GetValue<bool>())
			{
				Utility.DrawCircle(PixPosition(), 100, Color.Blue);
				if(Q.Level > 0)
					Utility.DrawCircle(PixPosition(), QPix.Range, QPix.IsReady() ? Color.Green : Color.Red);
			}

			if(Program.Menu.Item("Draw_Q").GetValue<bool>())
				if(Q.Level > 0)
					Utility.DrawCircle(ObjectManager.Player.Position, Q.Range, Q.IsReady() ? Color.Green : Color.Red);

			if(Program.Menu.Item("Draw_W").GetValue<bool>())
				if(W.Level > 0)
					Utility.DrawCircle(ObjectManager.Player.Position, W.Range, W.IsReady() ? Color.Green : Color.Red);

			if(Program.Menu.Item("Draw_E").GetValue<bool>())
				if(E.Level > 0)
					Utility.DrawCircle(ObjectManager.Player.Position, E.Range, E.IsReady() ? Color.Green : Color.Red);

			if(Program.Menu.Item("Draw_R").GetValue<bool>())
				if(R.Level > 0)
					Utility.DrawCircle(ObjectManager.Player.Position, R.Range, R.IsReady() ? Color.Green : Color.Red);
		}

	}
}
